<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/10/22
 * Time: 16:01
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\common\business;


use app\common\lib\Show;
use app\common\lib\Status;
use app\common\model\mysql\Collection;
use app\common\validate\Venue as ValObj;
use app\common\model\mysql\Venue as ModelObj;
use think\Exception;
use think\facade\Log;

class Venue extends Base
{
    /**
     * @var ValObj
     */
    protected ValObj $valObj;
    /**
     * @var ModelObj
     */
    protected ModelObj $modelObj;

    /**
     *
     */
    public function __construct()
    {
        $this->valObj = new ValObj();
        $this->modelObj = new ModelObj();
    }

    public function add(array $data = [])
    {
        if (empty($data['address'])) {
            throw new Exception("请选择定位");
        }
        $addr = Show::json_decode($data['address_data']);

        $data['address_lat'] = $addr['location']['lat'];
        $data['address_lng'] = $addr['location']['lng'];
        return parent::add($data);
    }

    /**
     * @param int $limit
     * @param array $address
     * @param string $field
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getVenueListData(int $limit = 10, array $address = [], string $field = "*")
    {
        $lists = $this->modelObj->field($field)->where('status', Status::mysql_normal)
            ->paginate($limit)->toArray();
        if (!empty($lists['data'])) {
            $res_data = [];
            foreach ($lists['data'] as $k => $v) {
                $res_data[$k] = $v;
                $distance = Show::getDistance($v['address_lng'], $v['address_lat'], $address['longitude'], $address['latitude'], 2, 1);
                $res_data[$k]['distance'] = $distance;
            }
            $lists['data'] = $res_data;
            return $lists;
        } else {
            return $lists;
        }
    }

    /**
     * @param int $venueId
     * @param int $theatreCateId
     * @param int $time
     * @param string $openid
     * @return array
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderListByParam(int $venueId = 0, int $theatreCateId = 0, int $time = 0, string $openid = ""): array
    {

        if ($time == 0) {
            $time = time();
        }
        //查询剧院
        try {
            $venue = $this->getBaseById($venueId);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        if (!$venue) {
            throw new Exception("剧院不存在");
        }
        //查询剧场分类
        try {
            $cate = (new TheatreCate())->getBaseById($theatreCateId);
        } catch (\Exception $e) {
            throw new Exception("剧场查询错误");
        }
        if (!$cate) {
            throw new Exception("剧场错误");
        }
        //查询场次
        $where = [
            ['start_time', 'between', [
                strtotime(date('Y-m-d 00:00:00', $time)), strtotime(date('Y-m-d 23:59:59', $time))
            ]],
            ['venue', '=', $venueId],
            ["theatre_cate_id", '=', $cate['id']],
            ['start_time', '>', time()]
        ];
        try {
            $theatre = (new Theatre())->getTheatreByVenueId($where, '*,title as name,thumb as root_path_thumb,start_time as get_s_time,end_time as get_e_time');
        } catch (\Exception $e) {
            throw new Exception("场次错误");
        }
        $theatre_list = [];
        if (!empty($theatre)) {
            //查询是否收藏
            foreach ($theatre as $k => $v) {
                $theatre_list[$k] = $v;
                $collection = (new Collection())->getCollectionByOpenid($v['id'], $openid);
//                dump($collection);exit();
                if ($collection) {
                    $theatre_list[$k]['collection'] = 1;
                } else {
                    $theatre_list[$k]['collection'] = 0;
                }
            }
        }
        return $theatre_list;
    }
}